package com.fernfx.xingtan.common.pullrefresh;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseActivity;
import com.fernfx.xingtan.my.entity.HaveDomainEntity;
import com.fernfx.xingtan.my.entity.MyPocketMoneyHistroyEntity;
import com.fernfx.xingtan.my.entity.MyPrincipalEntity;
import com.fernfx.xingtan.my.entity.PurchaseHistroyEntity;
import com.fernfx.xingtan.my.entity.RetrunDomainEntity;
import com.fernfx.xingtan.utils.CollectionUtil;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by admin on 2018/8/20.
 * 下拉刷新的activity
 */

public class PullToRefreshActivity extends BaseActivity implements PullToRefreshContract.View {

    @BindView(R.id.pull_refresh_rlt)
    TwinklingRefreshLayout pullRefreshRlt;
    @BindView(R.id.pull_refresh_liv)
    ListView pullRefreshLiv;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    //url
    public static final String CALLBACK_KEY = "callBack";
    public PullToRefreshContract.Presenter presenter;
    public static final List<CallBack> CALLBACK_LIST = new ArrayList<>(2);
    private CallBack callBack;
    //零钱明细
    public static final int MY_POCKET_MONEY_STATUS = 0;
    //分红金额
    public static final int MY_PRINCIPAL_STATUS = 1;
    //购买记录
    public static final int PURCHASE_HISTROY_STATUS = 2;
    //拥有的领土
    public static final int HAVE_DOMAIN_STATUS = 3;
    //退还的领土
    public static final int RETRUN_DOMAIN_STATUS = 4;
    //adapter data
    private final List<Object> dataList = new ArrayList<>(3);
    //adpater
    public PullToRefreshAdapter pullToRefreshAdapter;
    //分页
    public int index = 1, count = 10;
    //true上拉刷新,false下拉加载
    public boolean loadFlag;
    //是否可以继续下拉
    public boolean isLoadMore = true;


    public static void start(Context context, CallBack callBack) {
        CALLBACK_LIST.clear();
        CALLBACK_LIST.add(callBack);
        Intent intent = new Intent(context, PullToRefreshActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_pull_refresh;
    }

    @Override
    protected void initView() {
        pullToRefreshAdapter = new PullToRefreshAdapter(mContext, dataList);
        pullRefreshLiv.setAdapter(pullToRefreshAdapter);
    }

    @Override
    protected void initData() {
        callBack = CALLBACK_LIST.get(CALLBACK_LIST.size() - 1);
        //set title
        setToolbarTitle(callBack.getTitle());
        presenter = new PullToRefreshPresenter();
        presenter.init(this);
        request();
    }

    @Override
    public BaseActivity getActivity() {
        return this;
    }

    @Override
    public void showHistroy(String jsonText) {
        if (loadFlag) {
            dataList.clear();
            pullRefreshRlt.finishRefreshing();
        } else {
            pullRefreshRlt.finishLoadmore();
        }
        //这次请求的是否有十条数据
        List<Object> requestList = new ArrayList<>(10);

        switch (callBack.getStatus()) {
            case MY_POCKET_MONEY_STATUS:
                //零钱明细
                MyPocketMoneyHistroyEntity myPocketMoneyHistroyEntity = FastJsonUtil.fromBean(jsonText, MyPocketMoneyHistroyEntity.class);
                List<MyPocketMoneyHistroyEntity.ObjBean.RecordsBean> recordsBeansList = myPocketMoneyHistroyEntity.getObj().getRecords();
                for (MyPocketMoneyHistroyEntity.ObjBean.RecordsBean recordsBean : recordsBeansList) {
                    dataList.add(recordsBean);
                    requestList.add(recordsBean);
                }
                pullToRefreshAdapter.setButtonDisplay(new PullToRefreshArgs.MyPmoneyButtonDisplay());
                pullToRefreshAdapter.notifyDataSetChanged();
                //set args
                PullToRefreshArgs.BaseRefreshListener myPmoneyBaseRefreshListener = new PullToRefreshArgs.BaseRefreshListener(this);
                pullRefreshRlt.setOnRefreshListener(myPmoneyBaseRefreshListener);
                break;
            case MY_PRINCIPAL_STATUS:
                MyPrincipalEntity myPrincipalEntity = FastJsonUtil.fromBean(jsonText, MyPrincipalEntity.class);
                List<MyPrincipalEntity.ObjBean.RecordsBean> principalList = myPrincipalEntity.getObj().getRecords();
                for (MyPrincipalEntity.ObjBean.RecordsBean recordsBean : principalList) {
                    dataList.add(recordsBean);
                    requestList.add(recordsBean);
                }
                pullToRefreshAdapter.setButtonDisplay(new PullToRefreshArgs.MyPrincipalButtonDisplay());
                pullToRefreshAdapter.notifyDataSetChanged();
                //set args
                PullToRefreshArgs.BaseRefreshListener myPrincipalBaseRefreshListener = new PullToRefreshArgs.BaseRefreshListener(this);
                pullRefreshRlt.setOnRefreshListener(myPrincipalBaseRefreshListener);
                break;
            case PURCHASE_HISTROY_STATUS:
                //购买记录
                PurchaseHistroyEntity purchaseHistroyEntity = FastJsonUtil.fromBean(jsonText, PurchaseHistroyEntity.class);
                List<PurchaseHistroyEntity.ObjBean.RecordsBean> purchaseHistroyList = purchaseHistroyEntity.getObj().getRecords();
                for (PurchaseHistroyEntity.ObjBean.RecordsBean recordsBean: purchaseHistroyList) {
                    dataList.add(recordsBean);
                    requestList.add(recordsBean);
                }
                pullToRefreshAdapter.setButtonDisplay(new PullToRefreshArgs.PurchaseHistroyButtonDisplay());
                pullToRefreshAdapter.notifyDataSetChanged();
                //set args
                PullToRefreshArgs.BaseRefreshListener purchaseHistroyBaseRefreshListener = new PullToRefreshArgs.BaseRefreshListener(this);
                pullRefreshRlt.setOnRefreshListener(purchaseHistroyBaseRefreshListener);
                break;
            case HAVE_DOMAIN_STATUS:
                //拥有的领土
                HaveDomainEntity haveDomainEntity = FastJsonUtil.fromBean(jsonText, HaveDomainEntity.class);
                List<HaveDomainEntity.ObjBean.RecordsBean> haveDomainList = haveDomainEntity.getObj().getRecords();
                for (HaveDomainEntity.ObjBean.RecordsBean recordsBean: haveDomainList) {
                    dataList.add(recordsBean);
                    requestList.add(recordsBean);
                }
                pullToRefreshAdapter.setButtonDisplay(new PullToRefreshArgs.HaveDomainButtonDisplay());
                pullToRefreshAdapter.notifyDataSetChanged();
                //set args
                PullToRefreshArgs.BaseRefreshListener haveDomainBaseRefreshListener = new PullToRefreshArgs.BaseRefreshListener(this);
                pullRefreshRlt.setOnRefreshListener(haveDomainBaseRefreshListener);
                break;
            case RETRUN_DOMAIN_STATUS:
                //退还的领土
                RetrunDomainEntity retrunDomainEntity = FastJsonUtil.fromBean(jsonText, RetrunDomainEntity.class);
                List<RetrunDomainEntity.ObjBean.RecordsBean> returnDomainList = retrunDomainEntity.getObj().getRecords();
                for (RetrunDomainEntity.ObjBean.RecordsBean recordsBean: returnDomainList) {
                    dataList.add(recordsBean);
                    requestList.add(recordsBean);
                }
                pullToRefreshAdapter.setButtonDisplay(new PullToRefreshArgs.RetrunDomainButtonDisplay());
                pullToRefreshAdapter.notifyDataSetChanged();
                //set args
                PullToRefreshArgs.BaseRefreshListener returnDomainBaseRefreshListener = new PullToRefreshArgs.BaseRefreshListener(this);
                pullRefreshRlt.setOnRefreshListener(returnDomainBaseRefreshListener);
                break;
        }
        //如果有十条说明还有下一页
        if (requestList.size() < 10) {
            isLoadMore = false;
        }
        //释放集合
        requestList.clear();
        requestList = null;

        //check data
        if (CollectionUtil.isEmpty(dataList)) {
            noDataTv.setVisibility(View.VISIBLE);
            pullRefreshRlt.setVisibility(View.GONE);
        } else {
            noDataTv.setVisibility(View.GONE);
            pullRefreshRlt.setVisibility(View.VISIBLE);
        }
    }

    public void request() {
        //request
        requestArgsMap.clear();
        requestArgsMap.putAll(callBack.getRequestArgsMap());
        requestArgsMap.put("pageNo", index);
        requestArgsMap.put("pageSize", count);
        presenter.request(callBack.getRequestUrl(), requestArgsMap, callBack.getIsGet());
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (presenter != null) {
            presenter.detachView();
        }
        if (!CollectionUtil.isEmpty(CALLBACK_LIST)) {
            CALLBACK_LIST.clear();
        }
        if (null != callBack) {
            callBack = null;
        }
        if (!CollectionUtil.isEmpty(dataList)) {
            dataList.clear();
        }
        if (pullToRefreshAdapter != null) {
            pullToRefreshAdapter = null;
        }
    }

    public interface CallBack extends Serializable {
        Map<String, Object> getRequestArgsMap();

        String getRequestUrl();

        boolean getIsGet();

        int getStatus();

        String getTitle();
    }
}
